FIND_PACKAGE(Lua REQUIRED)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${LUA_INCLUDE_DIRS})

FILE(GLOB HEADER src/*.h include/*.h)
FILE(GLOB SRC src/*.cpp *.rc)
FILE(GLOB UI ui/*.ui)
FILE(GLOB QRC *.qrc)
FILE(GLOB TS *.ts)
FILE(GLOB ICONS misc/*.icns)

IF(WITH_STATIC_EXTERNAL)
	# If using static Qt, also use static plugins
	ADD_DEFINITIONS(-DQT_STATICPLUGIN)
ENDIF(WITH_STATIC_EXTERNAL)

IF(APPLE)
	SET(MAC_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/misc)

	SET(MACOSX_BUNDLE_GUI_IDENTIFIER "org.melting-pot.cebmtpchat")
ENDIF(APPLE)

IF(PLUGIN)
	ADD_DEFINITIONS(-DQT_PLUGIN)
	ADD_DEFINITIONS(-DQT_SHARED)
	ADD_DEFINITIONS(-DQT_NO_DEBUG)
	SET_TARGET_PLUGIN(${TARGET} ${SRC} ${QRC} ${UI} ${HEADER} ${TS})
ELSE(PLUGIN)
	SET_TARGET_GUI_EXECUTABLE(${TARGET} ${SRC} ${QRC} ${UI} ${HEADER} ${TS} ${ICONS})
ENDIF(PLUGIN)

SET_TARGET_LABEL(${TARGET} ${PRODUCT})
TARGET_LINK_LIBRARIES(${TARGET} max ${LUA_LIBRARIES})

IF(APPLE)
	FIND_LIBRARY(FOUNDATION_FRAMEWORK Foundation)
	FIND_LIBRARY(COCOA_FRAMEWORK Cocoa)
	FIND_LIBRARY(CARBON_FRAMEWORK Carbon)
	FIND_LIBRARY(SYSTEMCONFIGURATION_FRAMEWORK SystemConfiguration)
	FIND_LIBRARY(SECURITY_FRAMEWORK Security)
	TARGET_LINK_LIBRARIES(${TARGET} ${FOUNDATION_FRAMEWORK} ${COCOA_FRAMEWORK} ${CARBON_FRAMEWORK} ${SYSTEMCONFIGURATION_FRAMEWORK} ${SECURITY_FRAMEWORK})
ENDIF(APPLE)

IF(MINGW)
	# resource compilation for MinGW
	ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ceb_rc.o
		COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR} -i${CMAKE_CURRENT_SOURCE_DIR}/ceb.rc
		-o ${CMAKE_CURRENT_BINARY_DIR}/ceb_rc.o )
	SET(CEB_SRCS ${CEB_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/ceb_rc.o)
ENDIF(MINGW)

IF(WITH_PCH)
  ADD_NATIVE_PRECOMPILED_HEADER(${TARGET} ${CMAKE_CURRENT_SOURCE_DIR}/src/common.h ${CMAKE_CURRENT_SOURCE_DIR}/src/common.cpp)
ENDIF(WITH_PCH)

IF(APPLE)
  # Manually copy all files in Mac OS X Bundle
  IF(WITH_RYZOM)
    ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND mkdir -p ${RESOURCES_DIR}/profiles)
    ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/default_*.xml ${RESOURCES_DIR}/profiles)
    ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/ceb_ryzom.ini ${RESOURCES_DIR}/ceb.ini)
  ELSE(WITH_RYZOM)
    ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/ceb.ini ${RESOURCES_DIR})
  ENDIF(WITH_RYZOM)

  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp -R ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/scripts ${RESOURCES_DIR})
  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp -R ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/modifiers ${RESOURCES_DIR})
  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp -R ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/resources ${RESOURCES_DIR})
  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/LICENCE ${RESOURCES_DIR})
  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/whatsnew.html ${RESOURCES_DIR})

  ADD_CUSTOM_COMMAND(TARGET ${TARGET} POST_BUILD COMMAND find ${RESOURCES_DIR} -name .svn -print0 | xargs -0 rm -rf)
ELSE(APPLE)
  IF(WITH_RYZOM)
    INSTALL(FILES share/ceb/ceb_ryzom.ini DESTINATION ${SHARE_PREFIX} RENAME ceb.ini)
    FILE(GLOB PROFILES share/ceb/default_*.xml)
    INSTALL(FILES ${PROFILES} DESTINATION ${SHARE_PREFIX}/profiles)
  ELSE(WITH_RYZOM)
    INSTALL(FILES share/ceb/ceb.ini DESTINATION ${SHARE_PREFIX})
  ENDIF(WITH_RYZOM)

  INSTALL(DIRECTORY share/ceb/scripts DESTINATION ${SHARE_PREFIX})
  INSTALL(DIRECTORY share/ceb/modifiers DESTINATION ${SHARE_PREFIX})
  INSTALL(DIRECTORY share/ceb/resources DESTINATION ${SHARE_PREFIX})
  INSTALL(FILES share/ceb/LICENCE share/ceb/whatsnew.html DESTINATION ${SHARE_PREFIX})

  IF(UNIX)
    INSTALL(FILES images/ceb.xpm DESTINATION share/pixmaps)
    INSTALL(FILES images/ceb_16x16.png DESTINATION share/icons/hicolor/16x16/apps RENAME ceb.png)
    INSTALL(FILES images/ceb_22x22.png DESTINATION share/icons/hicolor/22x22/apps RENAME ceb.png)
    INSTALL(FILES images/ceb_24x24.png DESTINATION share/icons/hicolor/24x24/apps RENAME ceb.png)
    INSTALL(FILES images/ceb_32x32.png DESTINATION share/icons/hicolor/32x32/apps RENAME ceb.png)
    INSTALL(FILES images/ceb_48x48.png DESTINATION share/icons/hicolor/48x48/apps RENAME ceb.png)
    INSTALL(FILES images/ceb_128x128.png DESTINATION share/icons/hicolor/128x128/apps RENAME ceb.png)
    INSTALL(FILES images/ceb.svg DESTINATION share/icons/hicolor/scalable/apps RENAME ceb.svg)
    INSTALL(FILES share/applications/ceb.desktop DESTINATION share/applications)
  ENDIF(UNIX)
ENDIF(APPLE)

IF(TARGET_X64)
  SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
ELSE(TARGET_X64)
  SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES")
ENDIF(TARGET_X64)

# packaging information
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${DESCRIPTION})
SET(CPACK_PACKAGE_VENDOR ${AUTHOR})
#SET(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_SOURCE_DIR}/README)
SET(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/share/ceb/LICENCE)
SET(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
SET(CPACK_PACKAGE_INSTALL_DIRECTORY ${PRODUCT})
SET(CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR};${PRODUCT};ALL;/")
SET(CPACK_PACKAGE_EXECUTABLES ${TARGET} ${PRODUCT})

# NSIS Specific Packing Setup
SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY ${PRODUCT})
SET(CPACK_NSIS_EXECUTABLES_DIRECTORY ".")
#SET(CPACK_NSIS_CREATE_ICONS "CreateShortCut '\$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\${PRODUCT}.lnk' '\$INSTDIR\\\\${TARGET}.exe'")
#SET(CPACK_NSIS_MODIFY_PATH ON)
SET(CPACK_NSIS_MUI_ICON ${CMAKE_CURRENT_SOURCE_DIR}/ceb.ico)
SET(CPACK_NSIS_MUI_UNIICON ${CMAKE_CURRENT_SOURCE_DIR}/ceb.ico)
SET(CPACK_NSIS_INSTALLED_ICON_NAME "${TARGET}.exe")
#SET(CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/res\\\\header.bmp") # 150x57
SET(CPACK_NSIS_DISPLAY_NAME ${PRODUCT})
SET(CPACK_NSIS_HELP_LINK "http://code.google.com/p/cebmtpchat/issues/list")
SET(CPACK_NSIS_URL_INFO_ABOUT "http://code.google.com/p/cebmtpchat")
SET(CPACK_NSIS_CONTACT "kervala@gmail.com")

## Source Packages
IF(WITH_RYZOM)
  SET(PACKAGE "${TARGET}_ryzom")
ELSE(WITH_RYZOM)
  SET(PACKAGE "${TARGET}")
ENDIF(WITH_RYZOM)

SET(PACKAGE "${PACKAGE}_${VERSION}")

IF(TARGET_X64)
  SET(PACKAGE "${PACKAGE}_amd64")
ENDIF(TARGET_X64)

SET(CPACK_PACKAGE_FILE_NAME ${PACKAGE})
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${TARGET}-${VERSION}")

IF(WIN32)
  SET(CPACK_GENERATOR "NSIS")
  SET(CPACK_SOURCE_GENERATOR "ZIP")
ELSE(WIN32)
  SET(CPACK_GENERATOR "TGZ")
  SET(CPACK_SOURCE_GENERATOR "TGZ")
ENDIF(WIN32)

set(CPACK_SOURCE_IGNORE_FILES
	"~$"
	"\\\\.hg"
	"\\\\.svn"
	"\\\\.cvsignore$"
	"^${CMAKE_SOURCE_DIR}.*/CVS/"
	"^${CMAKE_SOURCE_DIR}.*/\\\\.svn/"
	"^${CMAKE_SOURCE_DIR}/debian/"
	"^${CMAKE_SOURCE_DIR}/old/")

INCLUDE(CPack)
